package gw.com.android.ui.trade;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.ui.dialog.AssistantDialog;
import gw.com.android.ui.trade.Fragment.BaseOrderFragment;
import gw.com.android.ui.trade.Fragment.MarketOrderFragment;
import gw.com.android.ui.trade.Fragment.OrderFailFragment;
import gw.com.android.ui.trade.Fragment.OrderSucFragment;
import gw.com.android.ui.trade.Fragment.PendingOrderFragment;
import gw.com.android.ui.views.CommonTitleBar;
import gw.com.android.utils.MobclickEventUtlis;
import gw.com.android.utils.PackStatis;
import www.com.library.app.FragmentsManagerUtil;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.view.BtnClickListener;

/**
 * 市价开仓和挂单开仓界面逻辑
 *
 * @author Administrator
 */
@Deprecated
public class OrderCreateActivity extends TradeOrderActivity {

    private MarketOrderFragment mCreateOrderFragment;
    private PendingOrderFragment mPendingOrderFragment;

    @Override
    protected void setAcitivityParam() {
        super.setAcitivityParam();
        Intent mIntent = getIntent();
        if (mIntent != null) {
            Bundle bundle = getIntent().getBundleExtra("params");
            mFromType = bundle.getInt("fromType", AppContances.TRADE_TYPE_FORM_QUOTE);
            mUiCode = bundle.getInt("uiCode", 0);
            mTradeDir = bundle.getInt("orderDir");
            Log.i("showOrderActivity", "参数是：mUiCode" + mUiCode + ",mFromType" + mFromType);
        }
    }

    @Override
    public void initLayoutView() {
        super.initLayoutView();
        onSendQuote();
        mTitleBar.mTitleView.setVisibility(View.GONE);
        mTitleBar.addTab(getResources().getString(R.string.order_create_market), true);
        mTitleBar.addTab(getResources().getString(R.string.order_create_pending), false);
        mTitleBar.setTabSelectedListener(new CommonTitleBar.SimpleTabSelectedListener() {
            @Override
            protected void onTabSelected(int position) {
                if (position == 0) {
                    showOrderFragment();
                } else if (position == 1) {
                    showPendingOrderFragment();
                }
            }
        });
        new AssistantDialog().attach(this, mTitleBar, AssistantDialog.TRADE);
    }

    @Override
    protected void initViewData() {

    }

    /**
     * 返回bundle对象
     */
    private Bundle getBundle() {
        Bundle bundle = getIntent().getBundleExtra("params");
        bundle.putInt("uiCode", mUiCode);
        bundle.putInt("orderDir", mTradeDir);
        return bundle;
    }

    /**
     * 展示市价下单界面
     */
    public void showOrderFragment() {
        super.showOrderFragment();
        mTypeTag = AppContances.TAB_ID_CREATE_ORDER;
        mCurrentTag = AppContances.TAB_ID_CREATE_ORDER;
        mCreateOrderFragment = (MarketOrderFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if (null == mCreateOrderFragment) {
            mCreateOrderFragment = new MarketOrderFragment();
            mCreateOrderFragment.setArguments(getBundle());
            mCreateOrderFragment.setOrderStatusListener(new BaseOrderFragment.onOrderStatusListener() {

                @Override
                public void onSuc(DataItemDetail mDetail) {
                    Logger.i("showCreateOrderFragment onSuc----------------");
                    //					 mDetail.Dump();
                    GTConfig.instance().saveProductOrderLot(mDetail.getInt("mPrdCode"), mDetail.getString("mCurLot"));
                    GTConfig.instance().saveProductOrderRange(mDetail.getInt("mPrdCode"), mDetail.getString("rangePrice"));
                    hasChangeLot = true;
                    hasChangeRange = true;
                    mDetailItem.clear().append(mDetail);
                    mHandler.sendEmptyMessage(AppContances.HANDLER_RESULT_SUC);
                }

                @Override
                public void onFail(DataItemDetail mDetail) {
                    Logger.i("showCreateOrderFragment onFail----------------");
                    //					mDetail.Dump();
                    hasChangeLot = false;
                    hasChangeRange = false;
                    mDetailItem.clear().append(mDetail);
                    mHandler.sendEmptyMessage(AppContances.HANDLER_SERVER_FAIL);
                }

                @Override
                public void onFail() {
                    hideLoading();
                }

                @Override
                public void onBegin() {
                    if (mCurrentTag == AppContances.TAB_ID_SUC_ORDER && mCurrentTag == AppContances.TAB_ID_FAILURE_ORDER) {
                        return;
                    }
                    showLoading();
                }

                @Override
                public void onError(String errorMessage) {
                    if (!"".equals(errorMessage) && (mCurrentTag != AppContances.TAB_ID_FAILURE_ORDER && mCurrentTag != AppContances.TAB_ID_SUC_ORDER)) {
                        m_errorTextView.setText(errorMessage);
                        m_errorTextView.setVisibility(View.VISIBLE);
                    } else {
                        m_errorTextView.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onShowView() {
                    mProgressLayout.setVisibility(View.GONE);
                }
            });
            FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mCreateOrderFragment, mCurrentTag);
        }
        showFragment(mCreateOrderFragment);
        restoreTitleBar();
    }

    /**
     * 展示挂单下单界面
     */
    public void showPendingOrderFragment() {
        super.showOrderFragment();
        mTypeTag = AppContances.TAB_ID_PENDING_ORDER;
        mCurrentTag = AppContances.TAB_ID_PENDING_ORDER;
        mPendingOrderFragment = (PendingOrderFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if (null == mPendingOrderFragment) {
            mPendingOrderFragment = new PendingOrderFragment();
            mPendingOrderFragment.setArguments(getBundle());
            mPendingOrderFragment.setOrderStatusListener(new BaseOrderFragment.onOrderStatusListener() {

                @Override
                public void onSuc(final DataItemDetail mDetail) {
                    Logger.i("onSuc----------------");
                    hasChangeLot = true;
                    GTConfig.instance().saveProductOrderLot(mDetail.getInt("mPrdCode"), mDetail.getString("mCurLot"));
                    mDetailItem.clear().append(mDetail);
                    mHandler.sendEmptyMessage(AppContances.HANDLER_RESULT_SUC);
                }

                @Override
                public void onFail(final DataItemDetail mDetail) {
                    Logger.i("onFail----------------");
                    hasChangeLot = false;
                    mDetailItem.clear().append(mDetail);
                    mHandler.sendEmptyMessage(AppContances.HANDLER_SERVER_FAIL);
                }

                @Override
                public void onFail() {
                    hideLoading();
                }

                @Override
                public void onBegin() {
                    if (mCurrentTag == AppContances.TAB_ID_SUC_ORDER && mCurrentTag == AppContances.TAB_ID_FAILURE_ORDER) {
                        return;
                    }
                    showLoading();
                }

                @Override
                public void onError(String errorMessage) {
                    if (!"".equals(errorMessage) && (mCurrentTag != AppContances.TAB_ID_FAILURE_ORDER && mCurrentTag != AppContances.TAB_ID_SUC_ORDER)) {
                        m_errorTextView.setText(errorMessage);
                        m_errorTextView.setVisibility(View.VISIBLE);
                    } else {
                        m_errorTextView.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onShowView() {
                    mProgress.setVisibility(View.GONE);
                }
            });
            FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mPendingOrderFragment, mCurrentTag);
        }
        showFragment(mPendingOrderFragment);
        restoreTitleBar();
    }

    /**
     * 展示下单失败界面
     */
    public void showOrderFailFragment(final DataItemDetail mDetail) {
        super.showOrderFailFragment(mDetail);
        mCurrentTag = AppContances.TAB_ID_FAILURE_ORDER;
        mFailFragment = (OrderFailFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if (null == mFailFragment) {
            mFailFragment = new OrderFailFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable("mDetail", mDetail);
            mFailFragment.setArguments(bundle);
            mFailFragment.setBtnClickListener(new BtnClickListener() {

                @Override
                public void onBtnClick(int viewID) {
                    if (mTypeTag.equals(AppContances.TAB_ID_CREATE_ORDER)) {
                        showOrderFragment();
                    } else {
                        showPendingOrderFragment();
                    }
                }
            });
            FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mFailFragment, mCurrentTag);
        } else {
            mFailFragment.setData(mDetail);
        }
        m_errorTextView.setVisibility(View.GONE);
        showFragment(mFailFragment);
        showResultTitleBar();
    }

    /**
     * 展示下单成功界面
     */
    public void showOrderSucFragment(DataItemDetail mDetail) {
        super.showOrderSucFragment(mDetail);
        mCurrentTag = AppContances.TAB_ID_SUC_ORDER;
        mSucFragment = (OrderSucFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if (null == mSucFragment) {
            mSucFragment = new OrderSucFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable("mDetail", mDetail);
            mSucFragment.setArguments(bundle);
            mSucFragment.setSucClickListener(new BtnClickListener() {

                @Override
                public void onBtnClick(int viewID) {
                    if (viewID == R.id.btn_custom_left) {
                        if (mTypeTag.equals(AppContances.TAB_ID_CREATE_ORDER)) {
                            showOrderFragment();
                            MobclickEventUtlis.MobclickEvent(OrderCreateActivity.this, "Demo_TradeSuc_Tick_Tra");
                        } else {
                            MobclickEventUtlis.MobclickEvent(OrderCreateActivity.this, "Read_TradeSuc_Tick_Tra");
                            showPendingOrderFragment();
                        }
                    }
                }
            });
            FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mSucFragment, mCurrentTag);
        } else {
            mSucFragment.setSucData(mDetail);
        }
        m_errorTextView.setVisibility(View.GONE);
        showFragment(mSucFragment);
        showResultTitleBar();
        if (mDetail.getString("mTradeDir").equals(AppContances.TRADE_TYPE_BUY + "")) {
            PackStatis.getToService(PackStatis.EventAction.BUY.getValue(), PackStatis.EventCategory.TRADE.getValue(), null, "1");
        } else if (mDetail.getString("mTradeDir").equals(AppContances.TRADE_TYPE_SALE + "")) {
            PackStatis.getToService(PackStatis.EventAction.BUY.getValue(), PackStatis.EventCategory.TRADE.getValue(), null, "0");
        }
    }

    private void restoreTitleBar() {
        if (mTitleBar != null) {
            TabLayout tabs = mTitleBar.tabs;
            if (tabs != null) {
                tabs.setVisibility(View.VISIBLE);
            }
            TextView mTitleView = mTitleBar.mTitleView;
            if (mTitleView != null) {
                mTitleView.setVisibility(View.GONE);
            }
        }
    }

    private void showResultTitleBar() {
        if (mTitleBar != null) {
            TabLayout tabs = mTitleBar.tabs;
            if (tabs != null) {
                tabs.setVisibility(View.GONE);
            }
            mTitleBar.setAppTitle(R.string.order_result);
        }
    }

    @Override
    public void onSymbolUpdateNotify() {
        if (null != mCreateOrderFragment) {
            mCreateOrderFragment.onSymbolNotify();
        }
        if (null != mPendingOrderFragment) {
            mPendingOrderFragment.onSymbolNotify();
        }
    }

    @Override
    public void onSymbolNotify(DataItemDetail tickModel) {
        if (null != mCreateOrderFragment) {
            mCreateOrderFragment.onSymbolNotify(tickModel);
        }
        if (null != mPendingOrderFragment) {
            mPendingOrderFragment.onSymbolNotify(tickModel);
        }
    }

}
